package cn.xorange.commons.security.entity;

import cn.xorange.commons.security.constants.SecurityConstants;
import cn.xorange.commons.security.enums.SysType;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : yangjian
 * @date : 2023/11/25
 */
@Data
public class SecurityUser implements UserDetails, Serializable {

    private Long userId;
    /** 甜橙Id */
    private Long orangeId;

    /** appUser 推荐人Id */
    private Long parentId;
    /** 推荐人 tree_node 节点*/
    private String treeNode;

    private String openId;
    private String mpOpenId;
    private String unionId;
    private String sex;
    private String username;
    private String nickname;
    private String phone;
    private String email;
    private String birthday;
    private String zodiac;
    private String star;
    @JSONField(serialize = false)
    private String password;
    private String city;
    private String avatar;

    private String staffId;
    private Long orgId;
    private String orgNode;
    private Long tenantId;
    private String tenantName;

    private String status;
    private String delFlag;
    private Date expireDate;
    private Date createTime;
    /** 权限集合 */
    private HashSet<String> permissions;
    /** 角色集合 */
    private HashSet<String> roleKeys;
    private List<SecurityRole> roles;

    private String isAdmin = SecurityConstants.FLAG_NO;
    /** 启用 */
    private boolean enable = true;
    private String sysType;

    public void setSysType(SysType sysType){
        this.sysType = sysType.type();
    }

    @JSONField(serialize = false)
    List<SimpleGrantedAuthority> authorities;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities!=null){
            return authorities;
        }
        authorities = new ArrayList<>();
        // 权限
        if(permissions != null && !permissions.isEmpty() ) {
            authorities.addAll(permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
        }

        // 角色
        if(!Objects.isNull(roleKeys) && !roleKeys.isEmpty() ) {
            authorities.addAll(roleKeys.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
        }
        return authorities;
    }

    public boolean getIsAdmin(){
        return SecurityConstants.FLAG_YES.equals(this.isAdmin) || "true".equals(this.isAdmin);
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enable;
    }
}
